<?php

define('SCR','search');
require('global.php');
require_once(R_P . 'require/init.php');

if (!$login_name) {
	showAlert('您没有登录，无法进行此项操作。');
}




require_once(D_P.'data/cache/field.php');

$search = new Search();
$search->doIt();



/**
 * CMS搜索各种自定义模型
 *
 */
class Search
{

	var $type; //查询方式：模糊查询 OR 精确查询
	var $displaynum; //显示条目
	var $config;
	/**
	 * 要搜索的内容表
	 *
	 * @var string
	 */
	var $table;
	/**
	 * 内容表中可供搜索的字段
	 *
	 * @var string
	 */
	var $fielddb;

	protected $mid = 0;

	public function __construct()
	{
		global $very,$fielddb;
		$this->config = $very;
		!$this->config['searchmax'] && $this->config['searchmax']=3;
		$this->fielddb = $fielddb;
		//if(!$GLOBALS['keyword']) throwError('please input the keyword');
	}


	function doIt()
	{
		global $timestamp;
		$type = $_POST['s_type'] ? $_POST['s_type'] : 'simple';
		$this->mid = intval(getVal('mid'));
		!$this->mid && $this->mid = 1;

		if(getVal('keyword'))
		{
			//if(GetCookie("sh")) throwError('You can not search now,please wait a moment..');
			if (!isset($this->fielddb[$this->mid]) || !$this->mid)
			{
				throwError('searchmiderror');
			}
			$this->table = 'cms_content'. $this->mid;
			$this->fielddb = $this->fielddb[$this->mid];
			$this->config['searchtime'] && Cookie("sh","1",$this->config['searchtime']+$timestamp);

			if (getVal('method') == 2)
			//if (!empty($_POST['type']) && $_POST['type'] != 'all')
			{
				$searchMethod = 'searchType';
				//$searchMethod = $type."Result";
			}
			else
			{
				$searchMethod = 'searchContent';
			}
		}
		else
		{
			showAlert('请输入关键字');
			//	$searchMethod = $type."Show";
		}
		$this->$searchMethod();
		exit();
	}

	/**
	 * 按照文件搜索
	 *
	 */
	function SearchType()
	{
		global $db,$cate,$very,$catedb,$moduledb, $restype, $root, $children, $cate_select, $cate_allow, $login, $login_name;
		global $cssname;
		$type = getVal('type');
		$keyword = Char_cv(getVal('keyword'));

		$type_checked = 1;

		require_once(R_P . 'require/class_resource.php');
		$res = new Resource();
		$res->setType($type);
		$keywordSQL = explode(" ", $keyword);
		$SQLADD = array();
		foreach ($keywordSQL as $S)
		{
			if (empty($S)) {
				continue;
			}
			$SQLADD[] = "r.filetitle LIKE('%$S%') OR r.filename LIKE('%$S%')";
		}
		$SQLADD = implode(' AND ', $SQLADD);
		//$res->setWhere("(r.filetitle LIKE('%$keyword%') OR c.title LIKE('%$keyword%'))");
		$res->setWhere("($SQLADD)");
		
		$method = 2;
		$pageurl = "search.php?method=2&keyword=$keyword&typeid=$type";

		$filesize = getVal('filesize');
		$filesize_t = getVal('filesize_t');
		$starttime = getVal('starttime');
		$endtime = getVal('endtime');
		$publisher = getVal('publisher');
		$cid = getVal('cid');

		if ($cid) //按照目录
		{
			$res->setWhere('r.cid = ' . $cid);
			$pageurl .= "&cid=$cid";
		}
		if ($filesize) //文件大小
		{
			$filesize = $filesize*1000000; //转化为字节单位
			if ($filesize_t == 1)
			{
				$res->setWhere("r.size > $filesize");
			}
			else
			{
				$res->setWhere("r.size < $filesize");
			}
			$pageurl .= "&filesize_t=$filesize_t&filesize=$filesize";
		}
		if ($starttime) {
			$startArr = explode("-", $starttime);
			$startStamp = mktime(0, 0, 0, $startArr[1], $startArr[2], $startArr[0]);
			$res->setWhere("r.uploadtime > $startStamp");
			$pageurl .= "&starttime=$starttime";
		}
		//时间限制
		if ($endtime) {
			$endArr = explode("-", $endtime);
			$endStamp = mktime(0, 0, 0, $endArr[1], $endArr[2], $endArr[0]);
			$res->setWhere("r.uploadtime < $endStamp");
			$pageurl .= "&endtime=$endtime";
		}
		if ($publisher) {
			$res->setWhere("c.publisher='$publisher'");
			$pageurl .= "&publisher=$publisher";
		}

		$count = $res->getListNum($very['searchmax']);

		$page = intval($_GET['page']);
		$pagesize = 20;
		$numofpage = ceil($count/$pagesize);
		$page <= 1 && $page = 1;
		$page >= $numofpage && $page = $numofpage;
		
		$searchresult = $res->getList($page, $pagesize);
		
		$pages = numofpage($count, $page, $numofpage, $pageurl . '&');
		start();
		require Template('typeresult');
		footer();
	}

	/**
	 * 简单搜索
	 *
	 */
	function searchContent()
	{
		global $db,$cate,$very,$catedb,$moduledb, $restype, $root, $children, $login, $login_name, $cate_select, $cssname;

		$keyword = getVal('keyword');
		$method = getVal('method');
		$mid = getVal('mid');
		if(!$keyword)
		{
			showAlert('请输入关键字');
		}
		if (!empty($GLOBALS['cate_allow']))
		{
			$SQLAdd = ' cid IN(' . implode(',', $GLOBALS['cate_allow']) . ') ';
		}
		if (getVal('method') == 3) //搜索用户
		{
			$SQLAdd .=  " AND publisher='$keyword' AND ifpub='1' AND mid='$this->mid'";
		}
		else
		{
			$keyword = trim(str_replace(array('%','_'),array('\%','\_'),$keyword));
			$keywordArr = explode(" ", $keyword);
			$SQLADD = array();
			foreach ($keywordArr as $S)
			{
				if (empty($S)) {
					continue;
				}
				$SQLADD[] = "title LIKE('%$S%')";
			}
			$SQLADD = implode(' AND ', $SQLADD);
			$SQLAdd .= " AND $SQLADD AND ifpub='1' AND mid='$this->mid'";
			//$SQLAdd .= " AND title LIKE('%$keyword%') AND ifpub='1' AND mid='$this->mid'";
		}

		$cid = intval(getVal('cid'));


		if (!empty($cid))
		{
			$SQLAdd .= ' AND cid =' . $cid;
		}

		$pageurl = "search.php?method=$method&keyword=$keyword";

		$starttime = getVal('starttime');
		$endtime = getVal('endtime');
		$publisher = getVal('publisher');

		if ($starttime) {
			$startArr = explode("-", $starttime);
			$startStamp = mktime(0, 0, 0, $startArr[1], $startArr[2], $startArr[0]);
			$SQLAdd .= " AND postdate > $startStamp";
			$pageurl .= "&starttime=$starttime";
		}
		//时间限制
		if ($endtime) {
			$endArr = explode("-", $endtime);
			$endStamp = mktime(0, 0, 0, $endArr[1], $endArr[2], $endArr[0]);
			$SQLAdd .= " AND postdate < $endStamp";
			$pageurl .= "&endtime=$endtime";
		}
		if ($publisher) {
			$SQLAdd .= " AND publisher='$publisher'";
			$pageurl .= "&publisher=$publisher";
		}

		$rt = $db->get_one("SELECT COUNT(*) AS total FROM cms_contentindex WHERE $SQLAdd LIMIT {$this->config['searchmax']}");
		$total = $rt['total'];
		$pagesize = 20;
		$numofpage = ceil($total/$pagesize);		
		$page = intval(getVal('page'));
		if ($page <= 1)
		{
			$page = 1;
		}
		elseif ($page >= $numofpage)
		{
			$page = $numofpage;
		}
		$startnum = ($page - 1)*$pagesize;
		$rs = $db->query("SELECT * FROM cms_contentindex WHERE $SQLAdd LIMIT $startnum, $pagesize");
		//echo "SELECT * FROM cms_contentindex WHERE $SQLAdd LIMIT {$this->config['searchmax']}";
		//exit;
		$searchresult = array();
		$keyword = stripslashes($keyword);
		while ($result = $db->fetch_array($rs)) {
			foreach ($keywordArr as $k)
			{
				$result['title'] = str_replace($k,"<span style=\"background-color:#FFFF00;color:#FF0000;\">$k</span>",$result['title']); 
				//搜索关键字高亮显示
			}
			if($catedb[$result['cid']]['htmlpub']){
				$result['url'] = $very['htmdir'].'/'.$result['url'];
			}else{
				$result['url'] = 'view.php?tid='.$result['tid'].'&cid='.$result['cid'];
			}
			$result['cname'] = $catedb[$result['cid']]['cname'];
			$searchresult[] = $result;
		}

		$pages = numofpage($total, $page, $numofpage, $pageurl . '&');
		start();
		require Template('result');
		footer();
	}
}
?>